#!/bin/bash
#

###echo "Assign Filenames"

PAT=/etc/

FILE=modprobe.conf
FILE1=modprobe.tmp.1
FILE2=modprobe.tmp.2
FILE3=modprobe.tmp.3
FILEBAK=modprobe.conf.bak

FILE=$PAT$FILE
FILE1=$PAT$FILE1
FILE2=$PAT$FILE2
FILE3=$PAT$FILE3
FILEBAK=$PAT$FILEBAK

###echo "If modprobe.conf does not exist, exit quick"
if [ ! -e $FILE ]; then
  exit
fi

###echo "If modprobe.conf contains < 2 options lpfc lines, exit quick"
NUM=`cat $FILE | grep "options lpfc" | grep -v lpfcmpl | grep -v "#" | wc -l`
if [ $NUM -lt 2 ]; then
  exit
fi

###echo "Remove temporary files if they exist"
if [ -e $FILE1 ]; then
  rm $FILE1
fi
if [ -e $FILE2 ]; then
  rm $FILE2
fi
#if [ -e $FILE3 ]; then
#  rm $FILE3
#fi

###echo "Make a backup of the original modprobe.conf before we modify it"
cp $FILE $FILEBAK

###echo "If modprobe.conf lacks the Emulex Comment Line, add it at the end"
NUM=`grep -c "# Emulex lpfc options" $FILE`
if [ $NUM -eq 0 ]; then
  echo "# Emulex lpfc options" >> $FILE
fi

###echo "Perform Main File Processing Step 1"
### Read through the modprobe file, and split it into two temp files:

### FILE1: Extract all lines except lpfc options lines and any Emulex lpfc comment header.
cat $FILE | grep -v "options lpfc " | grep -v "Emulex lpfc options" > $FILE1 

### FILE2: Extract only the lpfc options lines.
cat $FILE | grep "options" | grep " lpfc " | grep -v "^#" > $FILE2

###echo "Perform Main File Processing Step 2"
### From the lines in Temp File 2, build a new lpfc options line
### NOTE: there are 15 Driver Params possible, maximum.
LINE="options lpfc " 
while read F
do
  TOKENS=`echo $F | wc -w`
  for ((LOOP=0; LOOP<$TOKENS-2; LOOP++))
  do
    TOK=$(($LOOP+3))
    if [ $TOK -eq 3 ]; then
      OPTION=`echo $F | awk '{ print $3 }'`
    elif [ $TOK -eq 4 ]; then
      OPTION=`echo $F | awk '{ print $4 }'`
    elif [ $TOK -eq 5 ]; then
      OPTION=`echo $F | awk '{ print $5 }'`
    elif [ $TOK -eq 6 ]; then
      OPTION=`echo $F | awk '{ print $6 }'`
    elif [ $TOK -eq 7 ]; then
      OPTION=`echo $F | awk '{ print $7 }'`
    elif [ $TOK -eq 8 ]; then
      OPTION=`echo $F | awk '{ print $8 }'`
    elif [ $TOK -eq 9 ]; then
      OPTION=`echo $F | awk '{ print $9 }'`
    elif [ $TOK -eq 10 ]; then
      OPTION=`echo $F | awk '{ print $10 }'`
    elif [ $TOK -eq 11 ]; then
      OPTION=`echo $F | awk '{ print $11 }'`
    elif [ $TOK -eq 12 ]; then
      OPTION=`echo $F | awk '{ print $12 }'`
    elif [ $TOK -eq 13 ]; then
      OPTION=`echo $F | awk '{ print $13 }'`
    elif [ $TOK -eq 14 ]; then
      OPTION=`echo $F | awk '{ print $14 }'`
    elif [ $TOK -eq 15 ]; then
      OPTION=`echo $F | awk '{ print $15 }'`
    elif [ $TOK -eq 16 ]; then
      OPTION=`echo $F | awk '{ print $16 }'`
    elif [ $TOK -eq 17 ]; then
      OPTION=`echo $F | awk '{ print $17 }'`
    fi  
    SPACE=" "
    LINE=$LINE$SPACE$OPTION
  done
done < $FILE2

#  append Emulex "lpfc header" and corresponding lpfc entry containing all options
echo "# Emulex lpfc options" >> $FILE1
echo $LINE >> $FILE1

###echo "Finally re-write the modprobe.conf file"
mv $FILE1 $FILE

# clean up temp files
rm -f $FILE2
